package com.jj.base;

import android.app.Application;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.support.annotation.StringRes;
import android.text.method.ScrollingMovementMethod;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jj.R;
import com.jj.util.JJUtils;

/**
 * Created by qmsoft05 on 2017/3/22.
 */

public class BaseApplication extends Application {
    private static BaseApplication mBaseApplication;
    private static WindowManager mWindowManager;

    @Override
    public void onCreate() {
        super.onCreate();
        mBaseApplication = this;
        mWindowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
    }

    public static BaseApplication application() {
        return mBaseApplication;
    }

    public void showToast(String msg) {
        Toast.makeText(mBaseApplication, msg, Toast.LENGTH_SHORT).show();
    }

    public void showToast(@StringRes int resId) {
        Toast.makeText(mBaseApplication, resId, Toast.LENGTH_SHORT).show();
    }

    /***
     * 自定义toast，需要添加SYSTEM_ALERT_WINDOW权限
     * @param _context x
     * @param _msg 提示的内容
     * @param _height 提示框高度
     * @param _showTime 显示时间
     */
    public void showCustomTopToast(Context _context, String _msg, int _height, long _showTime) {
        WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();
        wmParams.format= PixelFormat.RGBA_8888;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                    | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        } else {
            wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                    | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
        }
        wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        wmParams.gravity = Gravity.TOP;
        wmParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        wmParams.height= ViewGroup.LayoutParams.WRAP_CONTENT;
        wmParams.windowAnimations = R.style.custom_toast;
        final View _view = LayoutInflater.from(_context).inflate(R.layout.view_custom_top_toast, null);
        TextView tv_msg = (TextView) _view.findViewById(R.id.tv_msg);
        tv_msg.setMinHeight(_height);
        tv_msg.setMovementMethod(ScrollingMovementMethod.getInstance());
        tv_msg.setText(_msg);
        mWindowManager.addView(_view, wmParams);
        _view.postDelayed(new Runnable() {
            @Override
            public void run() {
                mWindowManager.removeView(_view);
            }
        }, _showTime);
    }

}
